﻿/*************************************************************************
 * 
 * Filename :category
 * 
 * steven hu   2010/3/12 9:40:29
 *  
 * http://www.cnblogs.com/huxj
 *   
 * 
 * Change History:
 * 
 * 
**************************************************************************/



using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hxj.Common;
using Hxj.Data;
using Hxj.Data.Common;
using Hxj.Web;
using RicciWebsite.Model;
using System.Data;

namespace RicciWebsite.admin.products
{
    public partial class category : RicciWebsite.Pages.AdminPage
    {

        /// <summary>
        /// 分类ID
        /// </summary>
        protected Guid? pcid;

        protected void Page_Load(object sender, EventArgs e)
        {
            string tempid = Request.QueryString["pcid"];
            if (Validator.IsGuid(tempid))
            {
                pcid = new Guid(tempid);
            }

            if (!IsPostBack)
            {
                Common.BindCategory(txtppcid);

                txtppcid.Items.Insert(0, new ListItem("根目录", new Guid().ToString()));

                if (pcid.HasValue)
                {
                    //修改
                    bindData();

                    btnNew.Visible = true;
                    btnDelete.Visible = true;

                }

            }
        }

        void bindData()
        {
            data_products_categories cateModel = RicciDatabase.Default.From<data_products_categories>().Where(data_products_categories._.pcid == pcid.Value.ToString()).ToFirst();

            if (null == cateModel)
            {
                JavaScript.MsgBox_Redirect_End("该分类不存在!", "category.aspx?" + Guid.NewGuid().ToString());
            }
            else
            {
                txtdescription.Text = cateModel.description;
                txtkeywords.Text = cateModel.keywords;
                txtmemo.Text = cateModel.memo;
                txtpcname.Text = cateModel.pcname;
                txtppcid.SelectedValue = cateModel.ppcid.ToString();
                txtpsort.Text = cateModel.psort.ToString();

            }

        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            if (!IsValid)
                return;


            data_products_categories cateModel = new data_products_categories();
            cateModel.Attach();
            EntityUtils.UpdateModel<data_products_categories>(cateModel, "txt");
            if (pcid.HasValue)
            {

                cateModel.DeAttach();
                cateModel.pcid = pcid.Value.ToString();
                RicciDatabase.Default.Update<data_products_categories>(cateModel);

                //Dictionary<Field, object> fields = new Dictionary<Field, object>();
                //fields.Add(data_products_categories._.description, txtdescription.Text.Trim());
                //fields.Add(data_products_categories._.keywords, txtkeywords.Text.Trim());
                //fields.Add(data_products_categories._.memo, txtmemo.Text.Trim());
                //fields.Add(data_products_categories._.pcname, txtpcname.Text.Trim());
                //fields.Add(data_products_categories._.ppcid, txtppcid.SelectedValue);
                //fields.Add(data_products_categories._.psort, txtpsort.Text.Trim());


                //RicciDatabase.Default.Update<data_products_categories>(fields, data_products_categories._.pcid == pcid.Value.ToString());

                JavaScript.MsgBox("修改成功!");
            }
            else
            {


                cateModel.pcid = Guid.NewGuid().ToString();
                cateModel.catetype = 1;
                RicciDatabase.Default.Insert<data_products_categories>(cateModel);

                JavaScript.MsgBox_Redirect_End("添加成功!", "category.aspx?" + Guid.NewGuid().ToString());
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkbtnDelete_Click(object sender, EventArgs e)
        {
            if (RicciDatabase.Default.Exists<data_products>(data_products._.pcid == pcid.Value.ToString()))
            {
                JavaScript.MsgBox("该分类下存在产品，不能删除！");
            }
            else
            {
                RicciDatabase.Default.Delete<data_products_categories>(pcid.Value.ToString());
                JavaScript.MsgBox_Redirect_End("删除成功!", "category.aspx?" + Guid.NewGuid().ToString());
            }

        }
    }
}
